package com.iss.yunpan.service.impl;

import com.iss.yunpan.dao.FileShareDao;
import com.iss.yunpan.entity.FileContent;
import com.iss.yunpan.service.FileShareService;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.HttpMethodEnum;
import com.obs.services.model.TemporarySignatureRequest;
import com.obs.services.model.TemporarySignatureResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.obs.services.ObsClient;
import com.iss.yunpan.service.impl.FileServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class FileShareServiceImpl implements FileShareService {
    @Autowired
    private FileShareDao dao;
    private HttpSession session;
    private String endPoint = "obs.cn-north-4.myhuaweicloud.com";
    private String ak = "KMFTH0TFIBTYIACKPJWT";
    private String sk = "LXPrmCt7N9yoUqzMmp8UppOipxvUPZkRfndWlhA1";
    private String bucketName = "yunpanst";

    @Override
    public List<FileContent> getAllShareFile(String fileName) {
        return dao.getAllShareFile(fileName);
    }

    @Override
    public Integer cancel(String fileAbsolute) {
        String objectKey = fileAbsolute;
        TemporarySignatureRequest request = new TemporarySignatureRequest();
        request.setBucketName(bucketName);
        request.setObjectKey(objectKey);
        request.setRequestDate(new Date());
        request.setExpires(1);
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
        String resultUrl = signature.getSignedUrl();

        System.out.println("获取临时URL~Get Temp Url:(preview):转码前url="+resultUrl);
        System.out.println("有效时间："+request.getExpires());
        System.out.println("数据分享时间："+request.getRequestDate());
        //在mysql相关table删除分享数据并返回修改数
        return dao.cancel(fileAbsolute);
    }

    @Override
    public String edit(String fileAbsolute, Integer timeRemaining) {
        String objectKey = fileAbsolute;
        TemporarySignatureRequest request = new TemporarySignatureRequest();
        request.setBucketName(bucketName);
        request.setObjectKey(objectKey);
        request.setRequestDate(new Date());
        request.setExpires(timeRemaining * 60);
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
        String resultUrl = signature.getSignedUrl();

        System.out.println("获取临时URL~Get Temp Url:(preview):转码前url="+resultUrl);
        System.out.println("有效时间："+request.getExpires());
        System.out.println("数据分享时间："+request.getRequestDate());
        Date time = new Date();
        dao.edit(fileAbsolute, timeRemaining, time);
        //在mysql相关table删除分享数据并返回修改数
        return resultUrl;
    }

}
